<?php
/**
 * @title 1629. 按键持续时间最长的键 - 哈希+迭代
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[] $releaseTimes
     * @param String $keysPressed
     * @return String
     */
    function slowestKey($releaseTimes, $keysPressed) {
        /**
         * @since 2020-10-26 长度
         */
        $len = count($releaseTimes);

        /**
         * @since 2020-10-26 字符第一次出现位置
         */
        $hashs = [
            $keysPressed[0] => 0,
        ];

        /**
         * @since 2020-10-26 最大时间, 按键组
         */
        $maxTime = $releaseTimes[0];
        $maxChar = $keysPressed[0];
        $maxKey = 0;

        /**
         * @since 2020-10-26 迭代
         */
        for($i=1; $i<$len; $i++){
            $char = $keysPressed[$i];
            if(!isset($hashs[$char])){
                $hashs[$char] = $i;
            } else {}
            $key = $hashs[$char];

            /**
             * @since 2020-10-26 间隔时间
             */
            $time = $releaseTimes[$i] -$releaseTimes[$i-1];
            // echo "char={$char}, time={$time}, key={$key}", PHP_EOL;

            if($time > $maxTime){
                $maxTime = $time;
                $maxChar = $char;
                $maxKey = $key;
            } elseif($time == $maxTime) {
                /**
                 * @since 2020-10-26 优先出现
                 */
                if($key < $maxKey){
                    $maxChar = $char;
                    $maxKey = $key;
                } else {}
            }
        }

        /**
         * @return
         */
        return $maxChar;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.26_2.php
 */
$datas = [
    [[9,29,49,50], "cbcd"], // c
    [[12,23,36,46,62], "spuda"], // a
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->slowestKey(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
